/* 
The plugin name:Custom leaderboards
author:Yoyo
v0.0.2
 */

var CONFIG = data.openConfig(
	".\\plugins\\Yoyo\\RankingList\\config.json",
	"json",
	JSON.stringify({
		count: 15,
		Scores: ["money"],
		List: [{ name: "Gold leaderboard", binding: "money", showFormat: "%R%,%P%-%S%gold" }, { name: "Perform CMD", type: "cmd", cmd: "/onlinetime rank" }],
	})
);
var DB = data.openDB(".\\plugins\\Yoyo\\RankingList\\database");


mc.listen("onScoreChanged", onScoreChanged);
mc.listen("onServerStarted", onServerStarted);


[
	{ cmd: "top", description: "-Open the leaderboard", callback: OpenRankingList, level: 0 },
	{ cmd: "top reload", description: "-Reload the leaderboard configuration file", callback: RankingListReload, level: 1 },
].forEach(i => mc.regPlayerCmd(i.cmd, i.description, i.callback, i.level));
//触发函数

function OpenRankingList(...ages) {
	utwHome(ages[0]);
}

function RankingListReload(...ages) {
	CONFIG.reload();
	ages[0].tell("§2Reloading the complete!");
}

function onScoreChanged(player, num, name) {
	let scoresArray = CONFIG.get("Scores");
	let recordScoresArray = scoresArray.filter(a => a == name);
	recordScoresArray.forEach(a => ScoreStorage(player.xuid, num, a));
}

function ScoreStorage(...ages) {
	if (mc.getScoreObjective(ages[2]) == null) {
		colorLog("red", `[Top]${ages[2]} -The score item does not exist`);
		return;
	}

	let scoreDbArray = DB.get(ages[2]);
	if (scoreDbArray == null) {
		DB.set(ages[2], [[ages[0], ages[1]]]);
		return true;
	}
	let playerScoreArr = scoreDbArray.filter(a => a[0] == ages[0]);
	if (playerScoreArr.length === 1) {
		scoreDbArray.forEach((v, k) => {
			if (v[0] == ages[0]) {
				scoreDbArray[k][1] = ages[1];
			}
		});
	} else {
		scoreDbArray.push([ages[0], ages[1]]);
	}
	DB.set(ages[2], scoreDbArray);
}

function utwHome(player) {
	let utw = mc.newSimpleForm();
	utw.setTitle("RankingList");
	let ButtonList = CONFIG.get("List");
	ButtonList.forEach(a => utw.addButton(a.name));
	player.sendForm(utw, (player, id) => {
		if (id == null && id !== 0) return;
		if (ButtonList[id] && ButtonList[id].type == "cmd") {
			player.runcmd(ButtonList[id].cmd);
		} else {
			utwShow(player, ButtonList[id]);
		}

	});
}

function utwShow(player, obj) {
	let utw = mc.newCustomForm();
	utw.setTitle(obj.name);
	utw.addLabel(repStr(obj));
	player.sendForm(utw, (player, data) => {
		if (data == null) return;
		utwHome(player);
	});
}

function repStr(obj) {
	let RankStr = "";
	let Vaarr = ranking(obj.binding);
	let ConfigCount = CONFIG.get("count");
	let conut = Vaarr.length > ConfigCount ? ConfigCount : Vaarr.length;
	for (let i = 0; i < conut; i++) {
		RankStr += obj.showFormat
			.replace(/%R%/g, i + 1)
			.replace(/%P%/g, !data.xuid2name(Vaarr[i][0]) ? "[Players don't exist]" : data.xuid2name(Vaarr[i][0]))
			.replace(/%S%/g, Vaarr[i][1]);
		RankStr += "§r\n";
	}
	return RankStr;
}

function ranking(binding) {
	let scoreDbArray = DB.get(binding);
	if (scoreDbArray == null) return [];
	return scoreDbArray.sort((a, b) => b[1] - a[1]);
}

function onServerStarted() {
	colorLog("green", "[Top] -Yoyo leaderboard loaded");
}
